package com.virtualperson.util;

import com.virtualperson.dto.ImageGenerationRequest;
import com.virtualperson.exception.ImageValidationException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class ParameterValidationUtil {
    // bgstyle_scale相关常量
    public static final float MIN_BGSTYLE_SCALE = 0.0f;
    public static final float MAX_BGSTYLE_SCALE = 1.0f;
    public static final float DEFAULT_BGSTYLE_SCALE = 0.7f;

    // realPerson相关常量
    public static final boolean DEFAULT_REAL_PERSON = true;

    // n相关常量
    public static final int MIN_N = 1;
    public static final int MAX_N = 4;
    public static final int DEFAULT_N = 1;

    // short_side_size相关常量
    public static final Set<String> V1_SHORT_SIDE_SIZES = new HashSet<>(Arrays.asList("512", "1024"));
    public static final Set<String> V2_SHORT_SIDE_SIZES = new HashSet<>(Arrays.asList("1024", "2048"));

    /**
     * 验证bgstyle_scale参数
     * @param bgstyleScale 背景参考图像权重控制值
     * @return 验证后的值
     * @throws ImageValidationException 如果值超出范围
     */
    public static float validateBgstyleScale(Float bgstyleScale) {
        if (bgstyleScale == null) {
            return DEFAULT_BGSTYLE_SCALE;
        }
        
        if (bgstyleScale < MIN_BGSTYLE_SCALE || bgstyleScale > MAX_BGSTYLE_SCALE) {
            throw new ImageValidationException(
                String.format("bgstyle_scale必须在%.1f到%.1f之间", MIN_BGSTYLE_SCALE, MAX_BGSTYLE_SCALE)
            );
        }
        
        return bgstyleScale;
    }

    /**
     * 验证realPerson参数
     * @param realPerson 是否真人标志
     * @return 验证后的值
     */
    public static boolean validateRealPerson(Boolean realPerson) {
        return realPerson != null ? realPerson : DEFAULT_REAL_PERSON;
    }

    /**
     * 验证n参数
     * @param n 生成图像的数量
     * @return 验证后的值
     * @throws ImageValidationException 如果值超出范围
     */
    public static int validateN(Integer n) {
        if (n == null) {
            return DEFAULT_N;
        }
        
        if (n < MIN_N || n > MAX_N) {
            throw new ImageValidationException(
                String.format("n必须在%d到%d之间", MIN_N, MAX_N)
            );
        }
        
        return n;
    }

    /**
     * 验证short_side_size参数
     * @param shortSideSize 短边大小
     * @param model 模型版本
     * @return 验证后的值
     * @throws ImageValidationException 如果值无效
     */
    public static String validateShortSideSize(String shortSideSize, String model) {
        if (shortSideSize == null || shortSideSize.trim().isEmpty()) {
            throw new ImageValidationException("short_side_size参数不能为空");
        }

        // 根据模型版本选择对应的有效值集合
        Set<String> validSizes = model.startsWith("v2") ? V2_SHORT_SIDE_SIZES : V1_SHORT_SIDE_SIZES;
        
        if (!validSizes.contains(shortSideSize)) {
            String validValues = String.join("、", validSizes);
            throw new ImageValidationException(
                String.format("模型%s的short_side_size参数值必须为：%s", model, validValues)
            );
        }
        
        return shortSideSize;
    }

    /**
     * 验证并设置默认参数值
     * @param request 图片生成请求
     */
    public static void validateAndSetDefaults(ImageGenerationRequest request) {
        if (request.getParameters() == null) {
            request.setParameters(new ImageGenerationRequest.Parameters());
        }
        
        // 验证并设置bgstyle_scale默认值
        if (request.getParameters().getBgstyleScale() == null) {
            request.getParameters().setBgstyleScale(DEFAULT_BGSTYLE_SCALE);
        }

        // 验证并设置n默认值
        if (request.getParameters().getN() == null) {
            request.getParameters().setN(DEFAULT_N);
        }

        // 验证并设置realPerson默认值
        if (request.getInput() == null) {
            request.setInput(new ImageGenerationRequest.Input());
        }
        if (request.getInput().getRealPerson() == null) {
            request.getInput().setRealPerson(DEFAULT_REAL_PERSON);
        }
    }
} 